package mylittleinvestigations.CaseInformation;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import mylittleinvestigations.Animation;
import mylittleinvestigations.Video;

public class AnimationManager
{
    private HashMap<String, Animation> animationByIdHashMap;
    private HashMap<String, Video> videoByIdHashMap;

    public AnimationManager()
    {
        this.animationByIdHashMap = new HashMap<String, Animation>();
        this.videoByIdHashMap = new HashMap<String, Video>();
    }

    public void AddAnimation(String animationId, Animation animation)
    {
        this.animationByIdHashMap.put(animationId, animation);
    }

    public Animation GetAnimationFromId(String animationId)
    {
        return this.animationByIdHashMap.get(animationId);
    }

    public void AddVideo(String animationId, Video animation)
    {
        this.videoByIdHashMap.put(animationId, animation);
    }

    public Video GetVideoFromId(String animationId)
    {
        return this.videoByIdHashMap.get(animationId);
    }

    void SaveToXml(CaseXmlWriter writer) throws IOException, CaseParseException
    {
        writer.StartElement("AnimationManager");
        writer.StartElement("AnimationByIdHashMap");
        
        String[] ids = this.animationByIdHashMap.keySet().toArray(new String[0]);
        
        for (int i = 0; i < ids.length; i++)
        {
            String id = ids[i];
            
            writer.StartElement("Entry");
            writer.WriteTextElement("Id", id);
            this.animationByIdHashMap.get(id).SaveToXml(writer);
            writer.EndElement();
        }
        
        writer.EndElement();
        writer.StartElement("VideoByIdHashMap");
        
        ids = this.videoByIdHashMap.keySet().toArray(new String[0]);
        
        if (ids.length > 0)
        {
            Files.createDirectory(Paths.get(Case.getInstance().getFilePath(), "Videos"));
        }
        
        for (int i = 0; i < ids.length; i++)
        {
            String id = ids[i];
            
            writer.StartElement("Entry");
            writer.WriteTextElement("Id", id);
            this.videoByIdHashMap.get(id).SaveToXml(writer);
            writer.EndElement();
        }
        
        writer.EndElement();
        writer.EndElement();
    }

    public AnimationManager(CaseXmlReader reader)
    {
        this.animationByIdHashMap = new HashMap<String, Animation>();
        
        reader.StartElement("AnimationManager");
        reader.StartElement("AnimationByIdHashMap");
        
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            Animation animation = new Animation(reader);
            
            this.animationByIdHashMap.put(id, animation);
        }
        
        reader.EndElement();
        reader.StartElement("VideoByIdHashMap");
        
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            Video animation = new Video(reader);
            
            this.videoByIdHashMap.put(id, animation);
        }
        
        reader.EndElement();
        reader.EndElement();
    }
}